package com.neuedu.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.neuedu.core.CharStorage;
import com.neuedu.service.Assistant;
import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import io.minio.MinioClient;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class AiConfig {
    @Value("${ai.api_key}")
    String apiKey;
    @Resource
    RedisConnectionFactory connectionFactory;
    @Value("${minio.url}")
    String url;
    @Value("${minio.username}")
    String username;
    @Value("${minio.password}")
    String password;
    @Bean
    MinioClient getMinioClient () {
        return MinioClient.builder()
                .endpoint(url)
                .credentials(username, password)
                .build();
    }
    @Bean(name = "qwen-plus")
    QwenChatModel getQwenChatModel () {
        return QwenChatModel.builder()
                .apiKey(apiKey)
                .modelName("qwen-plus")
                .build();
    }

    @Bean(name = "wycharstorage")
    ChatMemoryStore getChatMemoryStore () {
        return new CharStorage();
    }
    @Bean(name = "wyredistemplate")
    RedisTemplate<String, Object> redisTemplate () {
       RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
       redisTemplate.setConnectionFactory(connectionFactory);
       redisTemplate.setKeySerializer(new StringRedisSerializer());
       redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
       redisTemplate.setHashKeySerializer(new StringRedisSerializer());
       redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
       redisTemplate.afterPropertiesSet();
       return redisTemplate;
    }
    @Bean
    MybatisPlusInterceptor getInterceptor () {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}
